#define _CRT_SECURE_NO_WARNINGS 1

#include "Queue.h"

void QueInit(Que* pq)
{
	assert(pq);

	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}

void QueDestory(Que* pq)
{
	assert(pq);

	QNode* cur = pq->phead;
	while (cur)
	{
		QNode* tmp = cur->next;
		free(cur);
		cur = tmp;
	}
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

void QuePush(Que* pq, QDatatype x)
{
	QNode* newNode = (QNode*)malloc(sizeof(QNode));
	if (newNode == NULL)
	{
		perror("malloc");
		return;
	}
	newNode->data = x;
	newNode->next = NULL;

	if (pq->ptail == NULL)
	{
		assert(pq->phead == NULL);
		pq->phead = pq->ptail = newNode;
	}
	else
	{
		pq->ptail->next = newNode;
		pq->ptail = newNode;
	}
	pq->size++;
}

void QuePop(Que* pq)
{
	assert(pq);
	assert(!QueEmpty(pq));

	QNode* next = pq->phead->next;
	if (next == NULL)
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	else
	{
		free(pq->phead);
		pq->phead = next;
	}
	pq->size--;

}

int QueSize(Que* pq)
{
	assert(pq);
	return pq->size;
}

QDatatype QueFront(Que* pq)
{
	assert(pq);
	assert(!QueEmpty(pq));

	return pq->phead->data;
}

QDatatype QueBack(Que* pq)
{
	assert(pq);
	assert(!QueEmpty(pq));

	return pq->ptail->data;
}
bool QueEmpty(Que* pq)
{
	assert(pq);

	return pq->phead == NULL;
}
